// Mmanagement/filleUpload/filleUpload.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inpvalue:'请选择',
    hidden:false,
    inpclicknum:0,
    filetype:' ',
    tempFilePath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:13}).then(res=>{
      that.setData({
        xlselects:res
      })
    })
  },
  inpclick(){
    var inpclicknum=this.data.inpclicknum
    inpclicknum=inpclicknum+1
    this.setData({
        inpclicknum
    })
    if(inpclicknum%2==0){
        this.setData({
            hidden:false,
        })
    }else{
        this.setData({
            hidden:true,
        })
    }
  },
  xlclick(e){
    var shopname=e.target.dataset.shopname;
    var shopnumber=e.target.dataset.shopnumber;
    this.setData({
      inpvalue:shopnumber,
      hidden:false,
      inpclicknum:this.data.inpclicknum+1
    })
  },
  // 上传到服务器并返回路径
  fimgclick(){
    var that=this;
    wx.chooseMessageFile({
      count: 1,
      type:'all',
      success(res){
        wx.showLoading({
          title: '加载中'
        })
        var tempFilePath=res.tempFiles[0].path;
        var filetype=res.tempFiles[0].type;
        var filename=res.tempFiles[0].name;
        that.setData({
          filetype,
          filename
        })
        if(filetype=="image"){
          // 上传到服务器
          wx.uploadFile({
            filePath: tempFilePath,
            name: 'file',
            url: 'https://ipass.cc/myapi/Mmanagement/Fileupload/glyupload.php',
            success(res){
              wx.hideLoading();
              if(res.data=="图片大于2M"||res.data=="非合法的文件格式"){
                wx.showToast({
                  title: res.data,
                  icon:'none'
                })
              }
              var tempFilePath=res.data;
              that.setData({
                imgsrc:res.data,
                tempFilePath
              })
            }
          })
        }else if(filetype=="file"){
          wx.uploadFile({
            filePath: tempFilePath,
            name: 'file',
            url: 'https://ipass.cc/myapi/Mmanagement/Fileupload/glyupload.php',
            success(res){
              wx.hideLoading();
              if(res.data=="文件大于50M"||res.data=="非合法的文件格式"){
                wx.showToast({
                  title: res.data,
                  icon:'none'
                })
              }else{
                // 打开预览pdf
                var tempFilePath=res.data;
                that.setData({
                  tempFilePath
                })
                wx.downloadFile({
                  url: tempFilePath,   
                  success: function (res) {                           
                    if (res.statusCode === 200) { 
                      var Path = res.tempFilePath;
                      wx.openDocument({
                        fileType: 'pdf', 
                        filePath: Path,         
                        success: function (res) {
                          console.log('打开 PDF 成功');
                        }
                      })
                    }
                  },
                  fail: function (res) {
                    console.log(res);  
                  }
              })
              }
              
            }
          })
        }
      }
    })
  },
  // 上传到数据库
  upload(){
     var that=this;
     var tempFilePath=that.data.tempFilePath;
     var shopnumber=that.data.inpvalue;
     if(tempFilePath!=''){
      if(shopnumber=='请选择'){
        wx.showToast({
          title: '请选择要上传的店铺编号',
          icon:'none'
        })
      }else{
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'温馨提示',
          content:'确定要将文件上传到服务器吗？',
          success:res=>{
            if(res.confirm==true){
              //  发送请求
              var filetype=that.data.filetype;
              var filename=that.data.filename;
              app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:15,tempFilePath,shopnumber,filetype,filename}).then(res=>{
                wx.showToast({
                  title: res,
                  icon:'none'
                })
              })
            }
          }
        })
      }
     }else{
       wx.showToast({
         title: '请先点击上传预览',
         icon:'none'
       })
     }
  }
})